package edu.ynu.dao;
// default package

import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.Criteria;
import org.hibernate.LockMode;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.criterion.Example;
import org.hibernate.criterion.Expression;

import edu.ynu.po.CstActivity;
import edu.ynu.po.LinkPerson;

/**
 	* A data access object (DAO) providing persistence and search support for CstActivity entities.
 			* Transaction control of the save(), update() and delete() operations 
		can directly support Spring container-managed transactions or they can be augmented	to handle user-managed Spring transactions. 
		Each of these methods provides additional information for how to configure it for the desired type of transaction control. 	
	 * @see .CstActivity
  * @author MyEclipse Persistence Tools 
 */

public class CstActivityDAO extends BaseHibernateDAO  {
		 private static final Log log = LogFactory.getLog(CstActivityDAO.class);
		//property constants
	public static final String CUSTOMER_ID = "customerId";
	public static final String DATE = "date";
	public static final String PLACE = "place";
	public static final String TITLE = "title";
	public static final String DESCRIPTION = "description";
	public static final String REMARK = "remark";



    
    public void save(CstActivity transientInstance) {
        log.debug("saving CstActivity instance");
        try {
        	Session session = getSession();
        	session.clear();
        	Transaction trans = session.beginTransaction();
            session.save(transientInstance);
            trans.commit();
            session.close();
            log.debug("save successful");
        } catch (RuntimeException re) {
            log.error("save failed", re);
            throw re;
        }
    }
    
	public void delete(CstActivity persistentInstance) {
        log.debug("deleting CstActivity instance");
        try {
        	Session session = getSession();
        	Transaction trans = session.beginTransaction();
            session.delete(persistentInstance);
            trans.commit();
            session.close();
            log.debug("delete successful");
        } catch (RuntimeException re) {
            log.error("delete failed", re);
            throw re;
        }
    }
	
	public void update(CstActivity transientInstance){
		Session session = getSession();
		session.clear();
    	Transaction trans = session.beginTransaction();
        session.update(transientInstance);
        trans.commit();
        session.close();
	}
    
    public CstActivity findById( java.lang.Integer id) {
        log.debug("getting CstActivity instance with id: " + id);
        try {
            CstActivity instance = (CstActivity) getSession()
                    .get("edu.ynu.po.CstActivity", id);
            return instance;
        } catch (RuntimeException re) {
            log.error("get failed", re);
            throw re;
        }
    }
    
    
    public List findByExample(CstActivity instance) {
        log.debug("finding CstActivity instance by example");
        try {
            List results = getSession()
                    .createCriteria("CstActivity")
                    .add(Example.create(instance))
            .list();
            log.debug("find by example successful, result size: " + results.size());
            return results;
        } catch (RuntimeException re) {
            log.error("find by example failed", re);
            throw re;
        }
    }    
    
    public List findByProperty(String propertyName, Object value) {
      log.debug("finding CstActivity instance with property: " + propertyName
            + ", value: " + value);
      try {
         String queryString = "from CstActivity as model where model." 
         						+ propertyName + "= ?";
         Query queryObject = getSession().createQuery(queryString);
		 queryObject.setParameter(0, value);
		 return queryObject.list();
      } catch (RuntimeException re) {
         log.error("find by property name failed", re);
         throw re;
      }
	}

	public List findByCustomerId(Object customerId
	) {
		return findByProperty(CUSTOMER_ID, customerId
		);
	}
	
	public List findByDate(Object date
	) {
		return findByProperty(DATE, date
		);
	}
	
	public List findByPlace(Object place
	) {
		return findByProperty(PLACE, place
		);
	}
	
	public List findByTitle(Object title
	) {
		return findByProperty(TITLE, title
		);
	}
	
	public List findByDescription(Object description
	) {
		return findByProperty(DESCRIPTION, description
		);
	}
	
	public List findByRemark(Object remark
	) {
		return findByProperty(REMARK, remark
		);
	}
	

	public List findAll(Map paramMap) {
		log.debug("finding all CstActivity instances");
		try {
			String title = (String) paramMap.get("title");
			//String custNo = (String) paramMap.get("custNo");
			String custName = (String)paramMap.get("custName");
			String linkMan = (String) paramMap.get("linkMan");
			String chargeMan = (String)paramMap.get("chargeMan");
			String date = (String)paramMap.get("date");
			Criteria c = getSession().createCriteria(CstActivity.class);
			if(StringUtils.isNotEmpty(title)){
				c.add(Expression.like("title", "%"+title+"%"));
			}
			if(StringUtils.isNotEmpty(custName)){
				c.add(Expression.like("custNo", "%"+custName+"%"));
			}
			if(StringUtils.isNotEmpty(linkMan)){
				c.add(Expression.like("linkMan", "%"+linkMan+"%"));
			}
			if(StringUtils.isNotEmpty(chargeMan)){
				c.add(Expression.like("chargeMan", "%"+chargeMan+"%"));
			}
			if(StringUtils.isNotEmpty(date)){
				c.add(Expression.like("date", "%"+date+"%"));
			}
			return c.list();
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}
	
    public CstActivity merge(CstActivity detachedInstance) {
        log.debug("merging CstActivity instance");
        try {
            CstActivity result = (CstActivity) getSession()
                    .merge(detachedInstance);
            log.debug("merge successful");
            return result;
        } catch (RuntimeException re) {
            log.error("merge failed", re);
            throw re;
        }
    }

    public void attachDirty(CstActivity instance) {
        log.debug("attaching dirty CstActivity instance");
        try {
            getSession().saveOrUpdate(instance);
            log.debug("attach successful");
        } catch (RuntimeException re) {
            log.error("attach failed", re);
            throw re;
        }
    }
    
    public void attachClean(CstActivity instance) {
        log.debug("attaching clean CstActivity instance");
        try {
            getSession().lock(instance, LockMode.NONE);
            log.debug("attach successful");
        } catch (RuntimeException re) {
            log.error("attach failed", re);
            throw re;
        }
    }
}